package models

import (
	"fmt"
	"goodsroom/appconfig"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var DB *gorm.DB
var err error

func Init() {
	mysqlConf := appconfig.AppConf.MysqlConfig
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlConf.User, mysqlConf.Password, mysqlConf.Host, mysqlConf.Post, mysqlConf.DataBase)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		println("err")
	}
	log.Printf("mysql success")
	err = DB.AutoMigrate(&Goodsroom{})
	if err != nil {
		println("err")
	}
	log.Printf("数据迁移成功")
}
